#include "stdafx.h"
#include "sp~.h"
sp:: sp(){}	
sp::~sp(){exit();}	

BOOL sp::init()
{
	CClientDC dc(hwnd);
	mdc = new CDC;
	mdc1 = new CDC;
	mdc->CreateCompatibleDC(&dc);
	mdc1->CreateCompatibleDC(&dc);
	bmp = new CBitmap;
	bmp->CreateCompatibleBitmap(&dc,WIDTH,HEIGHT);
	mdc->SelectObject(bmp);
	delete bmp;
	GetCurrentDirectory(256,dir);

	t=0;d=0;
	n=1;ok=1;
	meetmonster=0;
	chmap=false;moving=false;
	man.x=7;man.y=7;

	font.CreateFont(
	   25,                        // nHeight
	   0,                        // nWidth
	   0,                         // nEscapement
	   0,                         // nOrientation
	   FW_BOLD,                   // nWeight
	   FALSE,                     // bItalic
	   FALSE,                     // bUnderline
	   0,                         // cStrikeOut
	   ANSI_CHARSET,              // nCharSet
	   OUT_DEFAULT_PRECIS,        // nOutPrecision
	   CLIP_DEFAULT_PRECIS,       // nClipPrecision
	   DEFAULT_QUALITY,           // nQuality
	   DEFAULT_PITCH | FF_SWISS,  // nPitchAndFamily
	   "Arial Bold");                  // lpszFacename
	mdc->SelectObject(&font);	
	
	return true;
}

void sp::exit()
{
	delete mdc,mdc1,map,bitmap;
	MCIWndClose(hMCI);
	delete hMCI;
}

BOOL sp::loadbmp(CString cc)
{	DeleteObject(bitmap);
	bitmap=(HBITMAP)LoadImage
				(AfxGetInstanceHandle(),
				 cc,IMAGE_BITMAP,				 			
				 0,0,
				 LR_LOADFROMFILE|LR_CREATEDIBSECTION
				);
	if(bitmap==NULL) return FALSE;
	DIBSECTION ds;
	BITMAPINFOHEADER &bm = ds.dsBmih;
	GetObject(bitmap,sizeof(ds),&ds);
	w = bm.biWidth;	
	h = bm.biHeight;
	return TRUE;
}

void sp::getpic(CString cc)
{if(loadbmp(cc))mdc1->SelectObject(bitmap);}

void sp::TransparentBlt2( HDC hdc0,	
					int nX0,int nY0,
					int nW0,int nH0,
				      HDC hdc1,
					int nX1,int nY1,
					int nW1,int nH1,
				      UINT Tcol)
{	
	HBITMAP hBMP  =CreateCompatibleBitmap(hdc0,nW0, nH0);
	HBITMAP mBMP  =CreateBitmap(nW0,nH0,1,1,NULL);	
	HDC	hDC   =CreateCompatibleDC(hdc0);
	HDC	mDC   =CreateCompatibleDC(hdc0);
	HBITMAP oldBMP =(HBITMAP)SelectObject(hDC, hBMP);
	HBITMAP oldmBMP=(HBITMAP)SelectObject(mDC, mBMP);
	if (nW0==nW1&&nH0==nH1)
		BitBlt(hDC,0,0,nW0,nH0,hdc1,nX1,nY1,SRCCOPY);
	else
		StretchBlt(hDC,0,0,nW0,nH0,hdc1,nX1,nY1,nW1,nH1,SRCCOPY);

	SetBkColor(hDC, Tcol);
	BitBlt(mDC,0,0,nW0,nH0,hDC,0,0,SRCCOPY);
	SetBkColor(hDC, RGB(0,0,0));
	SetTextColor(hDC, RGB(255,255,255));
	BitBlt(hDC,0,0,nW0,nH0,mDC,0,0,SRCAND);
	SetBkColor(hdc0,RGB(255,255,255));
	SetTextColor(hdc0,RGB(0,0,0));

	BitBlt(hdc0,nX0,nY0,nW0,nH0,mDC,0,0,SRCAND);
	BitBlt(hdc0,nX0,nY0,nW0,nH0,hDC,0,0,SRCPAINT);

	SelectObject(hDC, oldBMP);				
	DeleteDC(hDC);
	SelectObject(mDC, oldmBMP);
	DeleteDC(mDC);
	DeleteObject(hBMP);
	DeleteObject(mBMP);
}

void sp::rmap()
{
	FILE *f;
	cc.Format("%s/res/%d.map",dir,n);
	f = fopen(cc,"r");
	fscanf(f,"%d,%d",&mh,&mw);
	fscanf(f,"%d,%d",&s.x,&s.y);
	fscanf(f,"%d,%d",&mon.x,&mon.y);
	CClientDC dc(hwnd);
	delete map;
	temp = new CBitmap;
	temp->CreateCompatibleBitmap(&dc,mw*DP,mh*DP);
	map = new CDC;
	map->CreateCompatibleDC(&dc);
	map->SelectObject(temp);
	delete temp;
	int x;
	for(int i=0;i<=mh-1;i++)
		for(int j=0;j<=mw-1;j++)
		{
			fscanf(f,"%1d",&x);
			maze[i][j] = x;
			cc.Format("%s/res/%d.bmp",dir,x);
			getpic(cc);
			map->BitBlt(DP*j,DP*i,w,h,mdc1,0,0,SRCCOPY);
		}
	fclose(f);
}

void sp::wmap()
{
	FILE *f;
	cc.Format("%s/res/%d.map",dir,n);
	f = fopen(cc,"w");
	fprintf(f,"%d,%d\n",mh,mw);
	for(int i=0;i<=mh-1;i++)
		for(int j=0;j<=mw-1;j++)
		{
			fprintf(f,"%1d",maze[i][j]);
			if(j==mw-1)fprintf(f,"\n");
		}	
	fclose(f);
}

void sp::loading()
{
	CClientDC dc(hwnd);
	cc.Format("%s/res/loading.bmp",dir);
	getpic(cc);
	mdc->BitBlt(0,0,WIDTH,HEIGHT,mdc1,0,0,SRCCOPY);
	cc.Format("%s/res/title.bmp",dir);
	getpic(cc);
	TransparentBlt2(*mdc,(WIDTH-w)/2,(HEIGHT-h)/2,w,h,*mdc1,0,0,w,h,RGB(0,0,0));
	dc.BitBlt(0,0,WIDTH,HEIGHT,mdc,0,0,SRCCOPY);
/*	mdc->BitBlt(0,0,WIDTH,HEIGHT,mdc1,0,0,BLACKNESS);
	mdc->SetBkMode(TRANSPARENT);
	mdc->SetTextColor(RGB(255,255,255));
	cc="loading...";
	mdc->TextOut(440,450,cc);
	dc.BitBlt(0,0,WIDTH,HEIGHT,mdc,0,0,SRCCOPY);
*/
}

void sp::loadmap()
{
	loading();
	rmap();
//	wmap();
	cc.Format("%s/res/%d.rmi",dir,n);
	MCIWndClose(hMCI);
	delete hMCI;
	hMCI = MCIWndCreate(NULL, NULL,WS_POPUP
					|MCIWNDF_NOPLAYBAR|MCIWNDF_NOMENU
					,cc);
	MCIWndPlay(hMCI);
//	MCIWndClose(hMCI);
}